{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fff82b54-0526-400b-86c6-34e96eb8b344",
   "metadata": {},
   "source": [
    "# 气象大数据云平台的观测数据读取\n",
    "\n",
    "#### —— nmc_met_io程序库使用说明\n",
    "\n",
    "国家气象中心天气预报技术研发室    \n",
    "April, 2021    \n",
    "Kan Dai "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea4ffa0c-6cb5-4443-92b1-60e88c4dad0b",
   "metadata": {},
   "source": [
    "## Prepare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "96251b6d-b99d-4cd6-85b6-16100b83aa51",
   "metadata": {},
   "outputs": [],
   "source": [
    "# set up things\n",
    "%matplotlib inline\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6048c6ae-9ae9-4c03-9644-8c3c2eb61597",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import xarray as xr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "953346af-3c4c-4876-86c9-fe29fd62b5dd",
   "metadata": {},
   "source": [
    "## 读取单站观测数据  \n",
    "常用的站点观测数据集有: \n",
    "* SURF_CHN_MUL_MIN, 中国地面分钟数据\n",
    "* SURF_CHN_PRE_MIN, 中国地面分钟数据\n",
    "* SURF_CHN_MUL_HOR, 中国地面逐小时资料\n",
    "* SURF_CHN_MUL_HOR_N, 中国地面逐小时(国家站)\n",
    "* SURF_CHN_MUL_DAY, 中国地面日值数据\n",
    "* SURF_CHN_MUL_DAY_N, 中国地面日值数据(国家站)\n",
    "* SURF_CHN_MUL_PEN, 中国地面候值数据\n",
    "* SURF_CHN_MUL_TEN, 中国地面旬值数据\n",
    "* SURF_CHN_MUL_TEN, 中国地面月值数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39d5d187-7209-4e7d-9e7e-fb64e31615a5",
   "metadata": {},
   "source": [
    "### 读取单站的逐小时观测数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dcad8a15-0b25-435b-a1bf-c780652f907f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Station_Id_C</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Alti</th>\n",
       "      <th>Year</th>\n",
       "      <th>Mon</th>\n",
       "      <th>Day</th>\n",
       "      <th>Hour</th>\n",
       "      <th>PRS_sea</th>\n",
       "      <th>TEM</th>\n",
       "      <th>DPT</th>\n",
       "      <th>RHU</th>\n",
       "      <th>PRE_1h</th>\n",
       "      <th>PRE_12h</th>\n",
       "      <th>PRE_24h</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1031.5</td>\n",
       "      <td>8.8</td>\n",
       "      <td>2.6</td>\n",
       "      <td>65</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1031.8</td>\n",
       "      <td>9.8</td>\n",
       "      <td>2.2</td>\n",
       "      <td>59</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1031.9</td>\n",
       "      <td>10.1</td>\n",
       "      <td>2.2</td>\n",
       "      <td>58</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1031.7</td>\n",
       "      <td>10.5</td>\n",
       "      <td>2.3</td>\n",
       "      <td>57</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1030.8</td>\n",
       "      <td>10.9</td>\n",
       "      <td>3.4</td>\n",
       "      <td>60</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>715</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>19</td>\n",
       "      <td>1029.7</td>\n",
       "      <td>-2.2</td>\n",
       "      <td>-12.5</td>\n",
       "      <td>45</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>716</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>20</td>\n",
       "      <td>1029.5</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-11.5</td>\n",
       "      <td>48</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>717</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>21</td>\n",
       "      <td>1029.5</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>-12.1</td>\n",
       "      <td>46</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>718</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>22</td>\n",
       "      <td>1029.9</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>-11.5</td>\n",
       "      <td>52</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>719</th>\n",
       "      <td>54511</td>\n",
       "      <td>39.8061</td>\n",
       "      <td>116.4694</td>\n",
       "      <td>32.8</td>\n",
       "      <td>2021</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>23</td>\n",
       "      <td>1030.5</td>\n",
       "      <td>-3.1</td>\n",
       "      <td>-11.8</td>\n",
       "      <td>51</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>720 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    Station_Id_C      Lat       Lon  Alti  Year  Mon  Day  Hour  PRS_sea  \\\n",
       "0          54511  39.8061  116.4694  32.8  2021   11    1     0   1031.5   \n",
       "1          54511  39.8061  116.4694  32.8  2021   11    1     1   1031.8   \n",
       "2          54511  39.8061  116.4694  32.8  2021   11    1     2   1031.9   \n",
       "3          54511  39.8061  116.4694  32.8  2021   11    1     3   1031.7   \n",
       "4          54511  39.8061  116.4694  32.8  2021   11    1     4   1030.8   \n",
       "..           ...      ...       ...   ...   ...  ...  ...   ...      ...   \n",
       "715        54511  39.8061  116.4694  32.8  2021   11   30    19   1029.7   \n",
       "716        54511  39.8061  116.4694  32.8  2021   11   30    20   1029.5   \n",
       "717        54511  39.8061  116.4694  32.8  2021   11   30    21   1029.5   \n",
       "718        54511  39.8061  116.4694  32.8  2021   11   30    22   1029.9   \n",
       "719        54511  39.8061  116.4694  32.8  2021   11   30    23   1030.5   \n",
       "\n",
       "      TEM   DPT  RHU  PRE_1h  PRE_12h  PRE_24h  \n",
       "0     8.8   2.6   65     0.0      0.0      0.0  \n",
       "1     9.8   2.2   59     0.0      0.0      0.0  \n",
       "2    10.1   2.2   58     0.0      0.0      0.0  \n",
       "3    10.5   2.3   57     0.0      0.0      0.0  \n",
       "4    10.9   3.4   60     0.0      0.0      0.0  \n",
       "..    ...   ...  ...     ...      ...      ...  \n",
       "715  -2.2 -12.5   45     0.0      0.0      0.0  \n",
       "716  -2.0 -11.5   48     0.0      0.0      0.0  \n",
       "717  -2.0 -12.1   46     0.0      0.0      0.0  \n",
       "718  -3.0 -11.5   52     0.0      0.0      0.0  \n",
       "719  -3.1 -11.8   51     0.0      0.0      0.0  \n",
       "\n",
       "[720 rows x 15 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# import retrieve function\n",
    "from nmc_met_io.retrieve_cmadaas import cmadaas_obs_by_time_range_and_id\n",
    "\n",
    "# set retrieve parameters\n",
    "data_code = \"SURF_CHN_MUL_HOR_N\"  # 中国地面逐小时(国家站)\n",
    "time_range = \"[20211101000000,20211130230000]\"\n",
    "elements = \"Station_Id_C,Lat,Lon,Alti,Year,Mon,Day,Hour,PRS_sea,TEM,DPT,RHU,PRE_1h,PRE_12h,PRE_24h\"\n",
    "sta_ids = \"54511\"\n",
    "\n",
    "data = cmadaas_obs_by_time_range_and_id(time_range, data_code=data_code, elements=elements, sta_ids=sta_ids)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "522e5a5a-c0e7-4aab-a45c-aa3adbd9f0ae",
   "metadata": {},
   "source": [
    "如果需要读取比较长历史的观测记录, 由于大数据云平台存在下载数量限制, 可使用`retrieve_cmadass_history`模块中的函数逐年下载并拼接."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d707113e-84e8-428c-94ca-486bb33239b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Years: 100%|██████████| 11/11 [00:13<00:00,  1.22s/it]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Station_Id_d</th>\n",
       "      <th>Datetime</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Alti</th>\n",
       "      <th>TEM</th>\n",
       "      <th>DPT</th>\n",
       "      <th>RHU</th>\n",
       "      <th>PRE_1h</th>\n",
       "      <th>PRE_6h</th>\n",
       "      <th>PRE_24h</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>54511</td>\n",
       "      <td>2001-01-01 00:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-4.6</td>\n",
       "      <td>-9.0</td>\n",
       "      <td>72.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>54511</td>\n",
       "      <td>2001-01-01 01:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>54511</td>\n",
       "      <td>2001-01-01 02:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>54511</td>\n",
       "      <td>2001-01-01 03:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>54511</td>\n",
       "      <td>2001-01-01 04:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70075</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-31 19:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-6.3</td>\n",
       "      <td>-22.7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70076</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-31 20:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-6.3</td>\n",
       "      <td>-22.7</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70077</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-31 21:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-6.7</td>\n",
       "      <td>-23.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70078</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-31 22:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-6.8</td>\n",
       "      <td>-22.7</td>\n",
       "      <td>27.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70079</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-31 23:00:00</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-6.7</td>\n",
       "      <td>-23.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>70080 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Station_Id_d            Datetime   Lat       Lon  Alti  TEM   DPT   RHU  \\\n",
       "0            54511 2001-01-01 00:00:00  39.8  116.4667  31.3 -4.6  -9.0  72.0   \n",
       "1            54511 2001-01-01 01:00:00  39.8  116.4667  31.3  NaN   NaN   NaN   \n",
       "2            54511 2001-01-01 02:00:00  39.8  116.4667  31.3  NaN   NaN   NaN   \n",
       "3            54511 2001-01-01 03:00:00  39.8  116.4667  31.3  NaN   NaN   NaN   \n",
       "4            54511 2001-01-01 04:00:00  39.8  116.4667  31.3  NaN   NaN   NaN   \n",
       "...            ...                 ...   ...       ...   ...  ...   ...   ...   \n",
       "70075        54511 2010-12-31 19:00:00  39.8  116.4667  31.3 -6.3 -22.7  26.0   \n",
       "70076        54511 2010-12-31 20:00:00  39.8  116.4667  31.3 -6.3 -22.7  26.0   \n",
       "70077        54511 2010-12-31 21:00:00  39.8  116.4667  31.3 -6.7 -23.0  26.0   \n",
       "70078        54511 2010-12-31 22:00:00  39.8  116.4667  31.3 -6.8 -22.7  27.0   \n",
       "70079        54511 2010-12-31 23:00:00  39.8  116.4667  31.3 -6.7 -23.0  26.0   \n",
       "\n",
       "       PRE_1h  PRE_6h  PRE_24h  \n",
       "0         NaN     NaN      NaN  \n",
       "1         NaN     NaN      NaN  \n",
       "2         NaN     NaN      NaN  \n",
       "3         NaN     NaN      NaN  \n",
       "4         NaN     NaN      NaN  \n",
       "...       ...     ...      ...  \n",
       "70075     0.0     NaN      NaN  \n",
       "70076     0.0     NaN      NaN  \n",
       "70077     0.0     NaN      NaN  \n",
       "70078     0.0     NaN      NaN  \n",
       "70079     0.0     NaN      NaN  \n",
       "\n",
       "[70080 rows x 11 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# import retrieve function\n",
    "from nmc_met_io.retrieve_cmadaas_history import get_hist_obs_id\n",
    "\n",
    "# set retrieve parameters\n",
    "years = np.arange(2000, 2011,1)\n",
    "data_code = \"SURF_CHN_MUL_HOR_N\"    # 逐小时观测资料\n",
    "elements = 'Station_Id_d,Datetime,Lat,Lon,Alti,TEM,DPT,RHU,PRE_1h,PRE_6h,PRE_24h'\n",
    "sta_ids = \"54511\"\n",
    "\n",
    "# retrieve data\n",
    "data = get_hist_obs_id(years=years, data_code=data_code, elements=elements, sta_ids=sta_ids)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "113e3e1a-190b-43b7-9ecd-9a0c7f8fa127",
   "metadata": {},
   "source": [
    "## 读取单站的逐日观测数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3cf47a7e-9baa-407f-a450-60232bedcf65",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Years: 100%|██████████| 11/11 [00:05<00:00,  1.85it/s]\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Station_Id_d</th>\n",
       "      <th>Datetime</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Alti</th>\n",
       "      <th>TEM_Avg</th>\n",
       "      <th>TEM_Max</th>\n",
       "      <th>TEM_Min</th>\n",
       "      <th>PRE_Time_2020</th>\n",
       "      <th>PRE_Time_0808</th>\n",
       "      <th>WIN_S_2mi_Avg</th>\n",
       "      <th>WIN_S_Max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>54511</td>\n",
       "      <td>2000-01-01</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-1.1</td>\n",
       "      <td>1.3</td>\n",
       "      <td>-5.1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.8</td>\n",
       "      <td>2.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>54511</td>\n",
       "      <td>2000-01-02</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>0.7</td>\n",
       "      <td>4.6</td>\n",
       "      <td>-4.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.8</td>\n",
       "      <td>12.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>54511</td>\n",
       "      <td>2000-01-03</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-4.5</td>\n",
       "      <td>-0.3</td>\n",
       "      <td>-7.1</td>\n",
       "      <td>0.4</td>\n",
       "      <td>0.4</td>\n",
       "      <td>3.3</td>\n",
       "      <td>6.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>54511</td>\n",
       "      <td>2000-01-04</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-3.4</td>\n",
       "      <td>-1.9</td>\n",
       "      <td>-5.2</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.6</td>\n",
       "      <td>2.3</td>\n",
       "      <td>5.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>54511</td>\n",
       "      <td>2000-01-05</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-2.5</td>\n",
       "      <td>-1.5</td>\n",
       "      <td>-3.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4013</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-27</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-1.1</td>\n",
       "      <td>6.9</td>\n",
       "      <td>-7.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>6.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4014</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-28</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-1.9</td>\n",
       "      <td>2.5</td>\n",
       "      <td>-3.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.1</td>\n",
       "      <td>8.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4015</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-29</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-3.1</td>\n",
       "      <td>1.6</td>\n",
       "      <td>-5.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.9</td>\n",
       "      <td>11.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4016</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-30</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-6.4</td>\n",
       "      <td>-3.9</td>\n",
       "      <td>-7.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>8.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4017</th>\n",
       "      <td>54511</td>\n",
       "      <td>2010-12-31</td>\n",
       "      <td>39.8</td>\n",
       "      <td>116.4667</td>\n",
       "      <td>31.3</td>\n",
       "      <td>-6.2</td>\n",
       "      <td>-2.9</td>\n",
       "      <td>-8.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.3</td>\n",
       "      <td>9.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4018 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Station_Id_d   Datetime   Lat       Lon  Alti  TEM_Avg  TEM_Max  TEM_Min  \\\n",
       "0           54511 2000-01-01  39.8  116.4667  31.3     -1.1      1.3     -5.1   \n",
       "1           54511 2000-01-02  39.8  116.4667  31.3      0.7      4.6     -4.4   \n",
       "2           54511 2000-01-03  39.8  116.4667  31.3     -4.5     -0.3     -7.1   \n",
       "3           54511 2000-01-04  39.8  116.4667  31.3     -3.4     -1.9     -5.2   \n",
       "4           54511 2000-01-05  39.8  116.4667  31.3     -2.5     -1.5     -3.2   \n",
       "...           ...        ...   ...       ...   ...      ...      ...      ...   \n",
       "4013        54511 2010-12-27  39.8  116.4667  31.3     -1.1      6.9     -7.8   \n",
       "4014        54511 2010-12-28  39.8  116.4667  31.3     -1.9      2.5     -3.9   \n",
       "4015        54511 2010-12-29  39.8  116.4667  31.3     -3.1      1.6     -5.2   \n",
       "4016        54511 2010-12-30  39.8  116.4667  31.3     -6.4     -3.9     -7.7   \n",
       "4017        54511 2010-12-31  39.8  116.4667  31.3     -6.2     -2.9     -8.9   \n",
       "\n",
       "      PRE_Time_2020  PRE_Time_0808  WIN_S_2mi_Avg  WIN_S_Max  \n",
       "0               0.0            0.0            1.8        2.6  \n",
       "1               0.0            0.0            5.8       12.1  \n",
       "2               0.4            0.4            3.3        6.1  \n",
       "3               0.2            0.6            2.3        5.7  \n",
       "4               3.4            3.0            2.0        3.2  \n",
       "...             ...            ...            ...        ...  \n",
       "4013            0.0            0.0            1.9        6.4  \n",
       "4014            0.0            0.0            2.1        8.5  \n",
       "4015            0.0            0.0            3.9       11.0  \n",
       "4016            0.0            0.0            5.1        8.3  \n",
       "4017            0.0            0.0            6.3        9.4  \n",
       "\n",
       "[4018 rows x 12 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# import retrieve function\n",
    "from nmc_met_io.retrieve_cmadaas_history import get_hist_obs_id\n",
    "\n",
    "# set retrieve parameters\n",
    "years = np.arange(2000, 2011,1)\n",
    "data_code = \"SURF_CHN_MUL_DAY\"    # 逐小时观测资料\n",
    "elements = 'Station_Id_d,Datetime,Lat,Lon,Alti,TEM_Avg,TEM_Max,TEM_Min,PRE_Time_2020,PRE_Time_0808,WIN_S_2mi_Avg,WIN_S_Max'\n",
    "sta_ids = \"54511\"\n",
    "\n",
    "# retrieve data\n",
    "data = get_hist_obs_id(years=years, data_code=data_code, elements=elements, sta_ids=sta_ids)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7955298d-60b9-4c5a-b3cc-1aa0f056591f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Meteorology with Python",
   "language": "python",
   "name": "met"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
